﻿using System;
using System.Collections;
using System.Text;
using System.Data;
using GeneralLibs;
using System.Data.SqlClient;

namespace Lists
{
    public class KhoanMucCanLamSang : DoiTuong
    {
        #region Guid PhieuCanLamSangID property
        public Guid PhieuCanLamSangID
        {
            get
            {
                if (_DataRow["PhieuCanLamSangID"] is Guid) return (Guid)_DataRow["PhieuCanLamSangID"];
                return Guid.Empty;
            }
            set
            {
                _DataRow["PhieuCanLamSangID"] = value;
            }
        }
        #endregion

        #region Guid ThongSoCuaDichVuID property
        public Guid ThongSoCuaDichVuID
        {
            get
            {
                if (_DataRow["ThongSoCuaDichVuID"] is Guid) return (Guid)_DataRow["ThongSoCuaDichVuID"];
                return Guid.Empty;
            }
            set
            {
                _DataRow["ThongSoCuaDichVuID"] = value;
            }
        }
        #endregion

        #region string KetQua property
        public string KetQua
        {
            get
            {
                if (_DataRow["KetQua"] is string) return (string)_DataRow["KetQua"];
                return "";
            }
            set
            {
                _DataRow["KetQua"] = value;
            }
        }
        #endregion

        ////THứ tự là sort tên ABC
        //#region ThuTuKhoanMucTrongChungTu property
        //public int ThuTuKhoanMucTrongChungTu
        //{
        //    get
        //    {
        //        if (_DataRow["ThuTuKhoanMucTrongChungTu"] is int)
        //            return (int)_DataRow["ThuTuKhoanMucTrongChungTu"];
        //        else
        //            return 0;
        //    }
        //    set
        //    {
        //        _DataRow["ThuTuKhoanMucTrongChungTu"] = value;
        //    }
        //}
        //#endregion

        public KhoanMucCanLamSang(DataRow dataRow)
            : base(dataRow)
        {
        }
    }

    public class DanhMucKhoanMucCanLamSang : DanhMucDoiTuong, IEnumerable
    {
        #region Instance property: Singleton Design pattern
        private static DanhMucKhoanMucCanLamSang _Instance;
        public static DanhMucKhoanMucCanLamSang Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new DanhMucKhoanMucCanLamSang();
                    _Instance.DataView = _Instance.DataTable.DefaultView;
                    try
                    {
                        UniqueConstraint uniqueConstraint = new UniqueConstraint(_Instance.DataTable.Columns["Ma"]);
                        _Instance.DataTable.Constraints.Add(uniqueConstraint);
                        _Instance.DataView.Sort = "Ma";
                    }
                    catch (Exception ex)
                    {
                    }
                }
                return _Instance;
            }
        }
        #endregion

        #region override TableName property
        public override string TableName
        {
            get
            {
                return "Clinic_KhoanMucCanLamSang";
            }
        }
        #endregion

        #region Methods required by IEnumerable (use for foreach)
        public IEnumerator GetEnumerator()
        {
            return new TokenEnumerator(this);
        }

        private class TokenEnumerator : IEnumerator
        {
            private int _Position = -1;
            DanhMucDoiTuong _Dstk;

            public TokenEnumerator(DanhMucDoiTuong dstk)
            {
                _Dstk = dstk;
            }

            // Declare the MoveNext method required by IEnumerator:
            public bool MoveNext()
            {
                if (_Position < _Dstk.DataView.Count - 1)
                {
                    _Position++;
                    return true;
                }
                else
                {
                    return false;
                }
            }

            // Declare the Reset method required by IEnumerator:
            public void Reset()
            {
                _Position = -1;
            }

            // Declare the Current property required by IEnumerator:
            public object Current
            {
                get
                {
                    return new KhoanMucCanLamSang((_Dstk.DataView[_Position].Row));
                }
            }
        }
        #endregion

        public KhoanMucCanLamSang this[int index]
        {
            get
            {
                if (0 <= index && index <= this.Count)
                {
                    return new KhoanMucCanLamSang(DataTable.Rows[index]);
                }
                else
                {
                    return null;
                }
            }
        }

        public KhoanMucCanLamSang NewDoiTuong()
        {
            try
            {
                return new KhoanMucCanLamSang(Instance.DataTable.NewRow());
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public KhoanMucCanLamSang GetByProperty(string property, object value)
        {
            try
            {
                DataRow[] rows = GetDataRows(property, value);
                if (rows.Length > 0) return new KhoanMucCanLamSang(rows[0]);
                else return null;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public KhoanMucCanLamSang GetByID(Guid objID)
        {
            try
            {
                return GetByProperty("ID", objID);
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public static DanhMucKhoanMucCanLamSang GetAll()
        {
            try
            {
                return Instance;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public DanhMucKhoanMucCanLamSang Filter(string filterExpression)
        {
            try
            {
                DanhMucKhoanMucCanLamSang obj = new DanhMucKhoanMucCanLamSang();
                obj.DataView = new DataView(DanhMucKhoanMucCanLamSang.Instance.DataTable, filterExpression, "", DataViewRowState.CurrentRows);
                return obj;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public void LoadFromDB(Guid phieuCLSID)
        {
            SqlConnection con = new SqlConnection(GeneralLibs.LocalSettings.ConnectString);
            try
            {
                con.Open();
                SqlCommand cmd = con.CreateCommand();
                LoadFromDB(cmd, phieuCLSID);
            }
            catch (SqlException sqlEx)
            {
                throw new CustomException(CustomException.GetSqlExceptionMessage(sqlEx));
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
            finally
            {
                con.Close();
            }
        }
        public void LoadFromDB(SqlCommand cmd, Guid phieuCLSID)
        {
            try
            {
                cmd.CommandText = string.Format("SELECT * FROM Clinic_KhoanMucCanLamSang WHERE PhieuCanLamSangID = '{0}'", phieuCLSID);

                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable.Rows.Clear();
                da.Fill(DataTable);
                DataTable.AcceptChanges();
            }
            catch (SqlException sqlEx)
            {
                throw new CustomException(CustomException.GetSqlExceptionMessage(sqlEx));
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        //Tạo mới danh sách Khoản mục dựa vào DanhSachThongSoDichVu của dịch vụ tương ứng
        public void NewFromDanhSachThongSoDichVuDataTable(PhieuCanLamSang phieuCLS, DataTable danhSachThongSoDataTable)
        {
            try
            {
                DataTable.Clear();

                foreach (DataRow row in danhSachThongSoDataTable.Rows)
                {
                    DataRow newRow = DataTable.NewRow();
                    try
                    {
                        newRow["PhieuCanLamSangID"] = phieuCLS.ID;
                    }
                    catch (Exception ex)
                    { }
                    try
                    {
                        newRow["ThongSoCuaDichVuID"] = (Guid)row["ID"];
                    }
                    catch (Exception ex)
                    { }
                    try
                    {
                        newRow["KetQua"] = row["MoTa"].ToString();
                    }
                    catch (Exception ex)
                    { }

                    DataTable.Rows.Add(newRow);
                }
                DataTable.AcceptChanges();
            }
            catch (SqlException sqlEx)
            {
                throw new CustomException(CustomException.GetSqlExceptionMessage(sqlEx));
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }
    }
}
